package com.bmadmin.controller.zcgl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bmadmin.domain.Datagrid;
import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.NoticeDomain;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.domain.ZcFlwhDomain;
import com.bmadmin.domain.ZcwhDomain;
import com.bmadmin.service.ZcFlwhService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/zcflwh")
public class zcflwhController {
private Logger logger = LogManager.getLogger(this.getClass().getName());
	
    @Autowired
    ZcFlwhService zcflwhService;
    
    /**
     * 获取资产分类列表
     */
	@GetMapping("/getZcflList")
    public Object getZcflList(HttpSession session,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		PageHelper.startPage(page, limit);
		List<ZcFlwhDomain> list = zcflwhService.getZcflList(map);
		
		List<ZcFlwhDomain> list_new = new ArrayList(); 
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				ZcFlwhDomain zcfl = (ZcFlwhDomain)list.get(i);
				String glys = zcfl.getZcfl_gly();
				String[] gly = glys.split(",");
				String zcfl_gly_mc = "";
				for(int j=0;j<gly.length;j++){
					String gly_mc = zcflwhService.getGlymc(gly[j]);
					if("".equals(zcfl_gly_mc)){
						zcfl_gly_mc = gly_mc;
					}else{
						zcfl_gly_mc = zcfl_gly_mc +","+gly_mc;
					}
				}
				zcfl.setZcfl_gly_mc(zcfl_gly_mc);
				list_new.add(zcfl);
			}
		}
		
		PageInfo pagelist = new PageInfo(list_new);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list_new);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 获取用户列表
     */
    @GetMapping("/getZcfl")
    public LayUiDataTablePOJO getUserList(SysUsersPOJO sysUsersPOJO) {
        LayUiDataTablePOJO result = new LayUiDataTablePOJO();
        Map map = new HashMap();
        List<ZcFlwhDomain> list = zcflwhService.getZcflList(map);
        result.setCode(0);
        result.setMsg("查询成功");
        result.setData(list);
        result.setCount(list.size());
        logger.info(result);
        return result;
    }
	
    @PostMapping("/insertZcflwh")
    public NormalResult insertZcflwh(HttpSession session,ZcFlwhDomain zcflwh) {
    	String log = "资产分类新增>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcflwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcflwh.getZcfl_dm())){
        		result.setCode("9999");
                result.setMsg("分类代码为空！");
                logger.info(log+"分类代码为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcflwh.getZcfl_mc())){
        		result.setCode("9999");
                result.setMsg("分类名称为空！");
                logger.info(log+"分类名称为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcflwh.getZcfl_gly())){
        		result.setCode("9999");
                result.setMsg("资产管理员为空！");
                logger.info(log+"资产管理员为空！");
                return result;
        	}
        	int retResult = zcflwhService.insertZcflwh(zcflwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("保存成功！");
                logger.info(log+"保存成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("保存失败！");
                logger.info(log+"保存失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("资产分类保存异常！");
            logger.error(e);
        }
        return result;
    }
    
    @PostMapping("/deleteZcfl")
    public NormalResult deleteZcfl(String zcfl_dm) {
    	String log = "资产分类删除>>>>>>>>>>>";
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(zcfl_dm)) {
            result.setCode("9999");
            result.setMsg("传入ID为空!");
            return result;
        }

        int retResult = zcflwhService.deleteZcfl(zcfl_dm);
        if(retResult>0){
    		result.setCode("0000");
            result.setMsg("删除成功！");
            logger.info(log+"删除成功！");
    	}else{
    		result.setCode("9999");
            result.setMsg("删除失败！");
            logger.info(log+"删除失败！");
    	}
    	return result;
    }
    
    @PostMapping("/updateZcfl")
    public NormalResult updateZcfl(HttpSession session,ZcFlwhDomain zcflwh) {
    	String log = "资产分类更新>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcflwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcflwh.getZcfl_mc())){
        		result.setCode("9999");
                result.setMsg("分类名称为空！");
                logger.info(log+"分类名称为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcflwh.getZcfl_gly())){
        		result.setCode("9999");
                result.setMsg("资产管理员为空！");
                logger.info(log+"资产管理员为空！");
                return result;
        	}
        	int retResult = zcflwhService.updateZcfl(zcflwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("更新成功！");
                logger.info(log+"更新成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("更新失败！");
                logger.info(log+"更新失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("资产分类更新异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 获取资产分类信息
     */
    @GetMapping("/getZcflInfo")
    public NormalResult getZcflInfo(HttpSession session,String zcfl_dm){
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	NormalResult result = new NormalResult();
    	if (BMUtil.isNullOrEmpty(zcfl_dm)) {
            result.setCode("9999");
            result.setMsg("传入资产分类代码为空!");
            return result;
        }
    	Map map = new HashMap();
//    	map.put("userid", user.getU_id());
    	map.put("zcfl_dm",zcfl_dm);
    	List<ZcFlwhDomain> list = zcflwhService.getZcflList(map);
    	ZcFlwhDomain zcflwhDomain = list.get(0);
       
        result.setCode("0000");
        result.setMsg("success");
        result.setData(zcflwhDomain);
        return result;
    }
	
}
